package com.example.kekewaimaibao.core.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.kekewaimaibao.core.constant.RestConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author: xuxiang
 * @date: 2022/3/20 9:01 星期日
 * @description:
 * @version: 1.0
 */
@RestController
@RequestMapping(MailController.PATH)
public class MailController {

    @Autowired
    JavaMailSender mailSender;

    public static final String PATH = RestConstant.VERSION_V1 + "/mail";

    @RequestMapping("/send")
    public R sendQQEmail(@RequestParam String email, String title) {
        try {
            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom("2084532500@qq.com");
            helper.setTo(email);
            helper.setSubject(title);
            helper.setText("测试邮箱内容", false);
            this.mailSender.send(mimeMessage);
            return R.ok("发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            return R.ok("发送失败");
        }

    }
}
